#include "StdAfx.h"
#include "RotationOperateLogic.h"
#include "RotationIndicator.h"
#include "KeyEventManager.h"

CRotationOperateLogic* CRotationOperateLogic::mSelf = NULL ;

CRotationOperateLogic::CRotationOperateLogic(void):mbClick(false)
{
	dwRegisterState = emMouseMoveEvent | emMouseLeftBtnEvent | emMouseUpEvent ;
}


CRotationOperateLogic::~CRotationOperateLogic(void)
{
}

bool CRotationOperateLogic::MouseMoveEvent( SMOUSE_PARAM& mouse_param )
{
	CRotationIndicator* pRotationIndicator = CRotationIndicator::getSingletonPtr() ;
	Ogre::Camera* pCamera = CSceneCameraManager::getSingletonPtr()->getCameraPtr() ;

	if ( mbClick )
	{

		float fMoveDistance  = 
			mouse_param.fMouseX * mouse_param.fMouseX + mouse_param.fMouseY * mouse_param.fMouseY - 
			mfMouseX * mfMouseX - mfMouseY * mfMouseY ;

		fMoveDistance *= 10.0f ;

		Quaternion quat = Quaternion::IDENTITY ;
		Ogre::SceneNode* pNode = pRotationIndicator->GetSceneNode() ;

		switch(mnSelectAxes)
		{
		case RI_AXES_X:
			{
				quat.FromAngleAxis( Ogre::Radian( fMoveDistance ),pNode->getOrientation().yAxis() ) ;
			}
			break;
		case RI_AXES_Y:
			{
				quat.FromAngleAxis( Ogre::Radian( fMoveDistance ),pNode->getOrientation().xAxis() ) ;
			}
			break;
		case RI_AXES_Z:
			{
				quat.FromAngleAxis( Ogre::Radian( -fMoveDistance ),pNode->getOrientation().zAxis() ) ;
			}
			break;
		}

		if ( quat != Quaternion::IDENTITY )
		{
			pRotationIndicator->SetRotation( quat ) ;
		}
	}
	else
	{
		Ogre::Ray ray = pCamera->getCameraToViewportRay( mouse_param.fMouseX,mouse_param.fMouseY ) ;
		mnSelectAxes = pRotationIndicator->Intersect( ray ) ;
	}

	mfMouseX = mouse_param.fMouseX ;
	mfMouseY = mouse_param.fMouseY ;

	return true ;
}

bool CRotationOperateLogic::MouseLeftBtnClickEvent( SMOUSE_PARAM& mouse_param )
{
	if ( false == mbClick )
	{
		mfMouseX = mouse_param.fMouseX ;
		mfMouseY = mouse_param.fMouseY ;
	}
	mbClick = true ;
	return true ;
}

bool CRotationOperateLogic::MouseUpEvent( SMOUSE_PARAM& mouse_param )
{
	mbClick = false ;
	return true ;
}